const mysql=require('mysql')
const sqlInfo = require('../sql.config.js')

async function runSql(sql){
    conn = mysql.createConnection(sqlInfo)
    conn.connect()
    return new Promise((success,error)=>{
        conn.query(sql,(err,res)=>{
            if(err){
                console.log('connecting is erro'+err)
                return;
            }
            success({res:res,conn:conn})
        })
    })
}
async function insertSql(sql,data){
    conn = mysql.createConnection(sqlInfo)
    conn.connect()
    return new Promise((success,error)=>{
        conn.query(sql,data,(err,res)=>{
            if(err){
                console.log('connecting is erro')
                if(conn.state!='disconnected')
                    conn.end()
                return;
            }
            if(conn.state!='disconnected')
                conn.end()
            success(res)
        })
    })
}
module.exports = {
    runSql:runSql,
    insertSql:insertSql
}
